package com.situ.player.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;

import com.situ.player.activity.MusicPlayActivity;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Gao on 2022/6/15.
 */

public class MusicService extends Service{
    private static final String TAG = "MusicService";
    // Android提供的播放器，实现音乐的播放、暂停等功能
    private MediaPlayer mMediaPlayer;
    // 定时器
    private Timer mTimer;

    @Override
    public void onCreate() {
        super.onCreate();
        mMediaPlayer = new MediaPlayer();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        return new MusicControll();
    }

    // 这个类是通过onBind方法返回给Activity调用的
    public class MusicControll extends Binder{
        public void init(String path) {
            Log.d(TAG, "init: " + path);
            try {
                // 设置播放文件的路径
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(path);
                mMediaPlayer.prepare();
                mMediaPlayer.start();
                // 开启定时器，刷新播放进度
                addTimer();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // 由暂停到播放
        public void play() {
            Log.d(TAG, "play: ");
            mMediaPlayer.start();
        }

        // 由播放到暂停
        public void pause() {
            Log.d(TAG, "pause: ");
            mMediaPlayer.pause();
        }

        public boolean isPlaying() {
            boolean isPlaying = mMediaPlayer.isPlaying();
            Log.d(TAG, "isPlaying: " + isPlaying);
            return isPlaying;
        }

        public void seekTo(int progress) {
            Log.d(TAG, "progress: " + progress);
            mMediaPlayer.seekTo(progress);
        }
    }

    // 添加定时器，用于更新SeekBar的播放进度
    public void addTimer() {
        if (mTimer == null) {
            mTimer = new Timer();
            // 定时任务
            TimerTask task = new TimerTask() {

                @Override
                public void run() {
                    // 如果已经播放结束，直接退出
                    if (mMediaPlayer == null) {
                        return;
                    }
                    // 获取歌曲的总长度
                    int duration = mMediaPlayer.getDuration();
                    // 获取当前的播放进度
                    int currentPosition = mMediaPlayer.getCurrentPosition();
                    Message message = MusicPlayActivity.handler.obtainMessage();
                    // 将音乐的总时长和已经播放的时长放到Message
                    Bundle bundle = new Bundle();
                    bundle.putInt("duration", duration);
                    bundle.putInt("currentPosition", currentPosition);
                    message.setData(bundle);
                    MusicPlayActivity.handler.sendMessage(message);
                }
            };
            // 开启任务后延迟5毫秒执行第一次任务，以后每500毫秒再执行task任务
            mTimer.schedule(task, 5, 500);
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
        if (mMediaPlayer == null) {
            return;
        }
        // 如果正在循环播放，首先停止音乐
        if (mMediaPlayer.isLooping()) {
            mMediaPlayer.stop();
        }

        // 释放资源
        mMediaPlayer.release();
        mMediaPlayer = null;

    }
}
